package com.tjetc.reflect;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class _14Homework {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        /*
            练习2利用反射和File完成以下功能
            1.利用Class类的forName方法得到File类的class对象
            2.在控制台打印File类的所有构造器
            3.通过newInstance的方法创建File对象，并创建E:\mynew.txt文件
         */

//        Class clazz = File.class; 一般用于方法的传参
        Class clazz = Class.forName("java.io.File");
        Constructor[] constructors = clazz.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("constructor:" + constructor);
        }

        // 获取给路径完成的构造器
        Constructor declaredConstructor = clazz.getDeclaredConstructor(String.class);
        // File file = new File(路径)
        Object o = declaredConstructor.newInstance("/Users/gaoyongxiang/百度网盘/IDEA-Java/202507-java-se/chapter19/resource/1.txt");
        // file.createNewFile();
        Method createNewFile = clazz.getDeclaredMethod("createNewFile");
        createNewFile.invoke(o);
    }
}
